<?php

require_once('pub_inc.php');		//公共包含

$forum_id = 104;

$hotal = 'http://www.meituan.com/hotel/getcounterandpois/hengyang?ci='.date('Y-m-d', time()).'&co='.date('Y-m-d', time()+24*3600).'&page=';

for ($i = 1; $i <= 25; $i++) {
	$html_h = file_get_contents($hotal.$i, false, stream_context_create(array('http'=>array('header'=>"X-Requested-With:XMLHttpRequest\r\n"))));
	$h_s = json_decode($html_h)->data->dealsData;

	foreach ($h_s as $h_id => $h) {
		do_pick_content($h_id, $h);
	}
}

p_p_topic($forum_id);
return true;


function do_pick_content($h_id, $h) {
	global $forum_id;

	//避免频繁更新
	$topic = get_topic_by_order_l_n($h_id, $forum_id);
	if (check_topic_live($topic)) return true; 

	$html_c = file_get_contents('http://www.meituan.com/deal/'.$h_id.'.html');

	//提取内容
	if (preg_match('/仅售([^元]*)元/', $html_c, $m)) {
		$price = $m[1];
	} else return false; 

	if (preg_match('/<img class="focus-view" src="([^"]*)"/', $html_c, $m)) {
		$icon_url = $m[1];
	} 

	if (preg_match('/<div class=\'deal-term\'>([^`]*)<div id="anchor-reviews"/', $html_c, $m)) {
		$content = preg_replace('/data-src/', 'src', preg_replace('/src="data:[^"]*"|<p class="standard-bar"[^>]*>[^<]*<\/p>|<table width[^`]*<\/tbody>[^<]*<\/table>|<h2[^>]*>[^<]*<\/h2>/', '', $m[1]));
	}

	if (preg_match('/<div id="J-bizinfo-list" data-poi="([^"]*)"/', $html_c, $m)) {
		$adr = '';
		$adr_s = json_decode(preg_replace('/&quot;/','"', $m[1]));
		foreach ($adr_s as $a) {
			if ('hy' == $a->cityfirstalpha) {
				$adr .= $a->name.'<br>';
				$adr .= $a->address.'<br>';
				$adr .= $a->phone.'<br>';

				$name = $a->name;
				$adr_t = $a->address;
				$phone = $a->phone;
				break;
			}
		}
		$adr .= '<hr>';
	}
	$content = $adr.$content;

	//提取标题
	if (preg_match('/<span class="deal-component-title-prefix">([^<]*)<\/span>/', $html_c, $m)) {
		$title =  $m[1];
	} 
	$title .= $name.$h->title;

	if (preg_match('/至\D?(\d{4}.\d{1,2}.\d{1,2})/', $html_c, $m)) {
		$start_t_s = strtotime(preg_replace('/\./', '-', $m[1]));
	} else return false;

	if (preg_match('/name="calendarid" value="([^"]*)"/', $html_c, $m)) {
		$out_s_u = 'http://r.union.meituan.com/url/visit/?a=1&key=s3IK8ZAUbpkjSta1u6qDHC2EOcewxRFoPir&url=http%3A%2F%2Fwww.meituan.com%2Fdeal%2Fbuy%2F'.$h_id.'%3Fcalendarid%3D'.$m[1];
	}

	$price_org = $h->value;

	do_topic_add_ext_1_x($forum_id, $title, $content, $icon_url, $price, $price_org, $phone, $start_t_s, $out_s_u, $h_id, $topic['id']); 

	return true;
}
